/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "core/结构数据.h"
#include <线性代数/矢量计算.h>




void f_mat_平面构建(const S_Vec3Array* point, S_Vec4Array* plane) {
	uint32 num = point->count;
	if (num >= 3) {
		num /= 3;
		f_core_array_resize((S_Array*)plane, num);
		for (uint32 i = 0; i < num; ++i) {
			plane->ptr_userData[i] = f_graph_顶点构建平面(&point->ptr_userData[i * 3]);
		}
	}
	else {
		f_core_array_resize((S_Array*)plane, 0);
	}
}

void f_mat_点到平面距离(const S_Vec3Array* point, const S_Vec4Array* plane, S_F32Array* 距离) {
	uint32 num = DEF_Max(point->count, plane->count);

	bool 点单例 = num > point->count;
	bool 面单例 = num > plane->count;

	f_core_array_resize((S_Array*)距离, num);

	for (uint32 i = 0; i < num; ++i) {
		vec3 点;
		if (点单例) 点 = point->ptr_userData[0];
		else 点 = point->ptr_userData[i];

		vec4 平面;
		if (面单例) 平面 = plane->ptr_userData[0];
		else 平面 = plane->ptr_userData[i];

		距离->ptr_userData[i] = f_graph_点到平面距离(点, 平面);
	}
}

void f_mat_射线平面相交(const S_Vec3Array* point, const S_Vec4Array* plane, S_Vec3Array* 交点) {
	if (point->count <= 2) {
		f_core_array_resize((S_Array*)交点, 0);
		return;
	}

	uint32 num = DEF_Max(point->count / 2, plane->count);

	bool 点单例 = num > point->count / 2;
	bool 面单例 = num > plane->count;

	f_core_array_resize((S_Array*)交点, num);

	for (uint32 i = 0; i < num; ++i) {
		vec3 点[2];
		if (点单例) {
			点[0] = point->ptr_userData[0];
			点[1] = point->ptr_userData[1];
		}
		else {
			点[0] = point->ptr_userData[i * 2];
			点[1] = point->ptr_userData[i * 2 + 1];
		}

		vec4 平面;
		if (面单例) 平面 = plane->ptr_userData[0];
		else 平面 = plane->ptr_userData[i];

		f_graph_射线相交平面2(点[0], 点[1], 平面, 交点->ptr_userData[i]);
	}
}
